import {
  Body,
  Controller,
  Get,
  Post,
  Query,
  Res,
  UseGuards,
} from '@nestjs/common';
import { TemplateService } from './template_list.service';
import { TemplatesDto } from './dto';
import { ThrottlerProxyGuard } from '@/authGuard/throttler.guard';
import { JwtAuthGuard } from '@/authGuard/jwtAuthGuard';
import { CommonGuard } from '@/authGuard/common.guard';

@Controller()
export class TemplateController {
  constructor(private readonly templateService: TemplateService) {}
  @Post('addTemplate')
  addTemplates(@Body() params: TemplatesDto) {
    return this.templateService.addTemplates(params);
  }
  @Post('updateTemplates')
  updateTemplates(@Body() params: TemplatesDto) {
    return this.templateService.updateTemplates(params);
  }

  @Get('getTemplates')
  getTemplates(@Query() params) {
    return this.templateService.getTemplates(params);
  }

  @Get('getTemplatesByClassify')
  getTemplatesByClassify(@Query() params) {
    return this.templateService.getTemplatesByClassify(params);
  }

  @Post('getTemplateById')
  getTemplateById(@Body() params: { id: number }) {
    return this.templateService.getTemplateById(params);
  }

  @Post('getTemplateByMid')
  getTemplateByMid(@Body() params: { id: number }) {
    return this.templateService.getTemplateByMid(params);
  }

  @Post('updateStatus')
  updateStatus(@Body() params) {
    return this.templateService.updateStatus(params);
  }
  @Post('tempDel')
  tempDel(@Body() params) {
    return this.templateService.tempDel(params);
  }

  @Get('getCvTempList')
  getCvTempList(@Query() params) {
    return this.templateService.getCvTempList(params);
  }

  @Get('getCvTemp')
  getCvTemp(@Query() params) {
    return this.templateService.getCvTemp(params);
  }

  @Post('getWordTemplate')
  getWordTemplate(@Body() params) {
    return this.templateService.getWordTemplate(params);
  }
  @Post('getWordTemplateById')
  getWordTemplateById(@Body() params) {
    return this.templateService.getWordTemplateById(params);
  }

  @UseGuards(ThrottlerProxyGuard)
  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('downloadWord')
  async downloadWord(@Body() params: any, @Res() res) {
    res.setHeader('Access-Control-Expose-Headers', `Content-Disposition`);
    res.setHeader(
      'Content-Disposition',
      `attachment; filename="${encodeURIComponent(
        params?.fileName || '百捷简历',
      )}.docx"`,
    );
    res.setHeader('Content-Type', 'application/octet-stream,charset=UTF-8');
    const result = await this.templateService.downloadWord(params);
    res.end(result);
  }
}
